<template :model="SortingConfig">
  <a-drawer :title="SortingConfig.Name+'--叶片'" width="70%" :bordered="false" placement="right" :closable="false" :visible="visible" @close="onClose">
    <s-table ref="table" size="default" rowKey="Id" :columns="columns" :data="loadData" showPagination="auto">
      <span slot="Quantity" slot-scope="text, record">
        <template>
          <a-input-number v-model="record.Quantity" autocomplete="off" :min="1" :max="100" @blur="handleSaveQuantity(record)"/>
        </template>
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-action:Update @click="handleChooseSpec(record)">设置存放物料</a>
        </template>
      </span>
    </s-table>
    <EditForm ref="editForm" @Success="()=>{this.$refs.table.refresh()}"></EditForm>
    <ChooseSepcForm ref="chooseSepcForm" @Success="(spec)=>{this.handleSaveSpec(spec)}"></ChooseSepcForm>
  </a-drawer>
</template>

<script>
import moment from 'moment'
import { STable } from '@/components'
import MainSvc from '@/api/Define/MaterielSortingLocationSvc'
import EditForm from './Edit'
import EnumSelect from '@/components/CF/EnumSelect'
import EnumName from '@/components/CF/EnumName'
import ChooseSepcForm from '../MaterielSpec/Choose'

const columns = [
  { title: '料位编码', dataIndex: 'Location.Code', sorter: false },
  { title: '料位名称', dataIndex: 'Location.Name', sorter: false },
  { title: '物料规格编码', dataIndex: 'Spec.Code', sorter: false },
  { title: '物料规格名称', dataIndex: 'Spec.Name', sorter: false },
  { title: '料框容量', dataIndex: 'Quantity', scopedSlots: { customRender: 'Quantity' } },
  { title: '操作', dataIndex: 'action', width: '200px', scopedSlots: { customRender: 'action' } }
]

export default {
  components: {
    STable,
    MainSvc,
    EnumSelect,
    EnumName,
    EditForm,
    ChooseSepcForm
  },
  data() {
    this.columns = columns
    return {
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: { Keyword: '', SortingConfigId: '' },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        var _query = Object.assign({}, { ...this.queryParam })
        for (const key in _query) {
          if (moment.isMoment(_query[key])) {
            _query[key] = _query[key].format('YYYY-MM-DD')
          }
        }
        const requestParameters = Object.assign({ sortField: 'LocationId', sortOrder: 'ASC', Search: _query }, parameter)
        console.log('loadData request parameters:', requestParameters)
        return MainSvc.GetPage(requestParameters)
      },
      SelectedLocation: { Id: '' },
      SortingConfig: { Id: '' }
    }
  },
  filters: {
  },
  created() {},
  computed: {
  },
  methods: {
    moment,
    openDrawer(record) {
        this.visible = true
        this.SortingConfig = record
        this.resetSearchForm()
        this.$refs.table.refresh()
    },
    handleChooseSpec(location) {
      this.mdl = null
      this.visible = true
      this.SelectedLocation = location

      this.$refs.chooseSepcForm.openForm(this.SortingConfig.SortingMaterial)
    },
    handleSaveSpec(spec) {
      this.SelectedLocation.SpecId = spec.Id
      this.loading = true
        MainSvc.Save(this.SelectedLocation).then(result => {
          this.loading = false
          if (result.Success) {
            this.$message.success(result.Msg)
            this.resetSearchForm()
            this.$refs.table.refresh()
          } else {
            this.$message.error(result.Msg)
          }
        })
    },
    handleSaveQuantity(data) {
      if (data.Quantity != null) {
        this.loading = true
        MainSvc.Save(data).then(result => {
          this.loading = false
          if (result.Success) {
            this.$message.success(result.Msg)
          } else {
            this.$message.error(result.Msg)
          }
        })
      }
    },
    onClose() {
        this.visible = false
    },
    handleEdit(record) {
      this.visible = true
      this.mdl = { ...record }
      this.$refs.editForm.openForm(record.Id, '修改')
    },
    resetSearchForm() {
      this.queryParam = { SortingConfigId: this.SortingConfig.Id, Keyword: '' }
    }
  }
}
</script>
